<?php

namespace App\Admin\Repositories;

use App\Models\TeamMember as Model;
use Dcat\Admin\Repositories\EloquentRepository;
use Illuminate\Support\Facades\DB;

class TeamMember extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    public function getOptionsMemberListByTeamIds(array $teamIds) : array
    {
        $cityList = $this->eloquentClass::query()
            ->whereIn('team_id', $teamIds)
            ->select(['member_id', 'member_name'])->get()->toArray();
        $options = [];
        foreach ($cityList as $key => $val) {
            $options[$val['member_id']] = $val['member_name'];
        }

        return $options;
    }

    public function getMemberCnt(array $teamIds = []) : int
    {
        $query = $this->eloquentClass::query();
        if (!empty($teamIds)){
            $query->whereIn('team_id', $teamIds);
        }

        return (int) $query->count();
    }

    public function getMemberIdList(array $teamIds = [], int $status = \App\Models\TeamMember::MEMBER_STATUS_ON) : object
    {
        $query = $this->eloquentClass::query()->where('status', $status);
        if (!empty($teamIds)){
            $query->whereIn('team_id', $teamIds);
        }

        return $query->select('member_id')->get()->values();
    }

    public function getOptionsByTeamId(int $teamId) : object
    {
        return $this->eloquentClass::query()
            ->where('team_id', $teamId)
            ->get([DB::raw('member_id as id'), DB::raw('member_name as text')]);;
    }

    public function getMemberListByTeamIds(array $teamIds) : object
    {
        return $this->eloquentClass::query()->whereIn('team_id', $teamIds)->get();
    }

    public function getMemberListByMemberIds(array $memberIds) : object
    {
        return $this->eloquentClass::query()
            ->whereIn('member_id', $memberIds)
            ->get();
    }

    public function getAllMemberList()
    {
        $cityList = $this->eloquentClass::query()
            ->select(['member_id', 'member_name'])
            ->get();

        $options = [];
        foreach ($cityList as $key => $val) {
            $options[$val['member_id']] = $val['member_name'];
        }

        return $options;
    }
}
